package com.doit.day01

import scala.collection.immutable
import scala.collection.mutable.ArrayBuffer
import scala.util.control.Breaks
import scala.util.control.Breaks._

//break
object _07_循环的中断 {
  def main(args: Array[String]): Unit = {

    //写了一个1-10的for循环，然后呢，只想打印到5，后面就中断不打印了
    //加一个循环守卫
    //    for(elem <- 1 to 10 if elem <=5){
    //      println(elem)
    //    }

    //    for(elem <- 1 to 10 ){
    //      if (elem > 5){
    //        return
    //      }
    //    }


    try {
      //抛异常
      for (elem <- 1 to 10) {
        if (elem > 5) {
          throw new Exception()
        }
        println(elem)
      }
    } catch {
      case e: Exception =>
    }

    //break  ==> 包装了break的类
    Breaks.breakable(
      for (elem <- 1 to 10) {
        if (elem > 5) {
          Breaks.break()
        }
        println(elem)
      }
    )
    println("hello")


    breakable(
      for (elem <- 1 to 10) {
        if (elem > 5) {
          break()
        }
        println(elem)
      }
    )
    println("hello")


    //循环的返回值   数组里面
//    val ints: ArrayBuffer[Int] = new ArrayBuffer[Int]()
    val ints: immutable.IndexedSeq[Int] = for (elem <- 1 to 10) yield {
      elem
    }




  }

}
